<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单唯一标识" prop="orderSn">
      <el-input v-model="dataForm.orderSn" placeholder="订单唯一标识"></el-input>
    </el-form-item>
    <el-form-item label="收货人姓名" prop="receiverName">
      <el-input v-model="dataForm.receiverName" placeholder="收货人姓名"></el-input>
    </el-form-item>
    <el-form-item label="收货人详细地址" prop="receiverDetailAddress">
      <el-input v-model="dataForm.receiverDetailAddress" placeholder="收货人详细地址"></el-input>
    </el-form-item>
    <el-form-item label="收货人手机号" prop="receiverPhone">
      <el-input v-model="dataForm.receiverPhone" placeholder="收货人手机号"></el-input>
    </el-form-item>
    <el-form-item label="订单状态 -1：取消；0：新建,待付款；1：已付款；2：已送出；3：已送达；" prop="orderStatus">
      <el-input v-model="dataForm.orderStatus" placeholder="订单状态 -1：取消；0：新建,待付款；1：已付款；2：已送出；3：已送达；"></el-input>
    </el-form-item>
    <el-form-item label="确认收货状态 0：未确认；1：已确认" prop="confirmStatus">
      <el-input v-model="dataForm.confirmStatus" placeholder="确认收货状态 0：未确认；1：已确认"></el-input>
    </el-form-item>
    <el-form-item label="退款状态 0：未退款 1：已退款" prop="refundStatus">
      <el-input v-model="dataForm.refundStatus" placeholder="退款状态 0：未退款 1：已退款"></el-input>
    </el-form-item>
    <el-form-item label="订单备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="订单备注"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          orderSn: '',
          receiverName: '',
          receiverDetailAddress: '',
          receiverPhone: '',
          orderStatus: '',
          confirmStatus: '',
          refundStatus: '',
          remark: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          orderSn: [
            { required: true, message: '订单唯一标识不能为空', trigger: 'blur' }
          ],
          receiverName: [
            { required: true, message: '收货人姓名不能为空', trigger: 'blur' }
          ],
          receiverDetailAddress: [
            { required: true, message: '收货人详细地址不能为空', trigger: 'blur' }
          ],
          receiverPhone: [
            { required: true, message: '收货人手机号不能为空', trigger: 'blur' }
          ],
          orderStatus: [
            { required: true, message: '订单状态 -1：取消；0：新建,待付款；1：已付款；2：已送出；3：已送达；不能为空', trigger: 'blur' }
          ],
          confirmStatus: [
            { required: true, message: '确认收货状态 0：未确认；1：已确认不能为空', trigger: 'blur' }
          ],
          refundStatus: [
            { required: true, message: '退款状态 0：未退款 1：已退款不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '订单备注不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/order/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderSn = data.order.orderSn
                this.dataForm.receiverName = data.order.receiverName
                this.dataForm.receiverDetailAddress = data.order.receiverDetailAddress
                this.dataForm.receiverPhone = data.order.receiverPhone
                this.dataForm.orderStatus = data.order.orderStatus
                this.dataForm.confirmStatus = data.order.confirmStatus
                this.dataForm.refundStatus = data.order.refundStatus
                this.dataForm.remark = data.order.remark
                this.dataForm.createTime = data.order.createTime
                this.dataForm.updateTime = data.order.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/order/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'orderSn': this.dataForm.orderSn,
                'receiverName': this.dataForm.receiverName,
                'receiverDetailAddress': this.dataForm.receiverDetailAddress,
                'receiverPhone': this.dataForm.receiverPhone,
                'orderStatus': this.dataForm.orderStatus,
                'confirmStatus': this.dataForm.confirmStatus,
                'refundStatus': this.dataForm.refundStatus,
                'remark': this.dataForm.remark,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
